package com.sgb.jicai.weight;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.jicai.R;
import com.sgb.jicai.entity.PriceRulesBean;
import com.sgb.jicai.view.adapter.ProcurementCountPriceAdapter;
import com.sgb.link.utils.UnitListUtils;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/7/23
 * AUTHOR:F_Q
 * Description:集采-规格型号-采购量和价格组件
 */
public class ProcurementCountPrice extends RelativeLayout {

    private RecyclerView recyclerview;
    private ImageView add_price;
    private List<PriceRulesBean> list;
    private ProcurementCountPriceAdapter procurementcountpriceadapter;
    private LinearLayoutManager layoutManager;
    private String UnitNames = "";

    public ProcurementCountPrice(Context context) {
        this(context, null);
    }

    public ProcurementCountPrice(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProcurementCountPrice(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        list = new ArrayList<>();
        PriceRulesBean priceRulesBean = new PriceRulesBean("1");
        priceRulesBean.setDelete(false);//是否展示删除按钮
        list.add(priceRulesBean);
        LayoutInflater.from(context).inflate(R.layout.procurement_count_price, this);
        recyclerview = findViewById(R.id.recyclerview);
        add_price = findViewById(R.id.add_price);
        add_price.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //新增数据判断数据是否都有值
                DataProcessing(context);
                //添加view数据之后，处理ui布局
                addProcurementCountPrice();
            }
        });
        recyclerviewData(context);
    }

    //  添加和删除view数据之后，处理ui布局
    private void addProcurementCountPrice() {
        procurementcountpriceadapter.notifyDataSetChanged();
        recyclerview.scrollToPosition(list.size() - 1);
        if (list.size() >= 5) {
            add_price.setVisibility(GONE);
        } else {
            add_price.setVisibility(VISIBLE);
        }
        if (list.size() > 1) {
            for (int i = 0; i < list.size(); i++) {
                if (i == list.size() - 1) {
                    list.get(i).setDelete(true);
                } else {
                    list.get(i).setDelete(false);
                }
            }
        }
    }

    //集合处理
    private void recyclerviewData(Context context) {
        if (procurementcountpriceadapter == null) {
            procurementcountpriceadapter = new ProcurementCountPriceAdapter(context, list);
        }
        layoutManager = new LinearLayoutManager(context);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(procurementcountpriceadapter);
        procurementcountpriceadapter.setCallback(new ProcurementCountPriceAdapter.ProcurementCountPriceCallback() {
            @Override
            public void ProcurementCountPriceDeleta(int position) {
                list.remove(position);
                //删除view数据之后，处理ui布局
                addProcurementCountPrice();
            }
        });
    }

    //数据处理
    private void DataProcessing(Context context) {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (TextUtils.isEmpty(list.get(i).getIntervalStartValue())) {
                    MToast.showToast(context, "请输入第" + (i + 1) + "条起始量");
                    return;
                }
                if (TextUtils.isEmpty(list.get(i).getIntervalEndValue())) {
                    MToast.showToast(context, "请输入第" + (i + 1) + "条结束量");
                    return;
                }
                if (TextUtils.isEmpty(list.get(i).getPrice())) {
                    MToast.showToast(context, "请输入第" + (i + 1) + "条价格");
                    return;
                }
                if (Double.parseDouble(list.get(i).getIntervalEndValue()) <= Double.parseDouble(list.get(i).getIntervalStartValue())) {
                    MToast.showToast(context, "请检查第" + (i + 1) + "条结束采购量必须大于起始采购量");
                    return;
                }
            }
        }
        int Size = list.size() + 1;
        if (Size <= 5) {
            String endValue = list.get(list.size() - 1).getIntervalEndValue();
            if (!UnitListUtils.getUnitStart(UnitNames)) {
               //整数
                if (Long.parseLong(endValue)>=999999){
                    MToast.showToast(context,"结束采购量过大，请重新输入");
                    return;
                }
            } else {
              //小数
                if (Double.parseDouble(endValue)>=999999.999){
                    MToast.showToast(context,"结束采购量过大，请重新输入");
                    return;
                }
            }
            PriceRulesBean priceRulesBean = new PriceRulesBean(Size + "");

            if (endValue != null && endValue.contains(".")) {//判断是否包含小数点
                //小数
                priceRulesBean.setIntervalStartValue((Double.parseDouble(list.get(list.size() - 1).getIntervalEndValue()) + 1) + "");
            } else {
                //整数
                priceRulesBean.setIntervalStartValue((Long.parseLong(list.get(list.size() - 1).getIntervalEndValue()) + 1) + "");
            }
            priceRulesBean.setDelete(false);
            priceRulesBean.setPrice("");
            list.add(priceRulesBean);
            if (list.size() > 1) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == list.size() - 1) {
                        list.get(i).setDelete(true);
                    } else {
                        list.get(i).setDelete(false);
                    }
                }
            }
        }
    }

    //获取数据列表
    public List<PriceRulesBean> getData() {
        List<PriceRulesBean> datas = procurementcountpriceadapter.getDatas();
        return datas;
    }

    //设置数据
    public void setData(List<PriceRulesBean> priceRules) {
        if (list != null && list.size() > 0) {
            list.clear();
        }
        list.addAll(priceRules);
        if (list!=null&&list.size()>0){
            if (list.size() > 1) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setSort((i + 1) + "");
                    if (i == list.size() - 1) {
                        list.get(i).setDelete(true);
                    } else {
                        list.get(i).setDelete(false);
                    }
                }
            } else {
                list.get(0).setSort("1");
            }
        }
    }

    public void setUnit(String unitName) {
        if (procurementcountpriceadapter!=null){
            procurementcountpriceadapter.setUnit(unitName);
            UnitNames = unitName;
        }

    }
}
